package com.sky.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;

@Data
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
public class OssUtils {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;

    /**
     * 上传文件到 oss
     * @param bytes
     * @param objectName
     * @return
     */
    public String upload(byte[] bytes,String objectName){
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
        //文件访问路径规则 https://BucketName.Endpoint/ObjectName
        StringBuilder stringBuilder = new StringBuilder("https://");
        StringBuilder result = stringBuilder.append(bucketName).append(".").append(endpoint).append("/").append(objectName);
        log.info("上传OSS地址 {}",result.toString());
        return result.toString();
    }
}
